#include "thread.h"


Thread::Thread() {

    pthread_mutex_init(&mLock, NULL);
    pthread_cond_init(&mCondition, NULL);

}

Thread::~Thread() {
    pthread_mutex_destroy(&mLock);
    pthread_cond_destroy(&mCondition);
}


void Thread::start() {
    pthread_create(&mThread, NULL, startThread, this);
}

void* Thread::startThread(void* ptr) {
    Thread* thread = (Thread *) ptr;
    thread->mRunning = true;
    thread->handleRun(ptr);
    thread->mRunning = false;
}

int Thread::join() {
   if (!mRunning) {
       return 0;
   }
    return pthread_join(mThread, NULL);
}

void Thread::stop() {

}

void Thread::handleRun(void *ptr) {

}


void Thread::waitOnNotify() {
    pthread_mutex_lock(&mLock);
    pthread_cond_wait(&mCondition, &mLock);
    pthread_mutex_unlock(&mLock);
}

void Thread::notify() {
    pthread_mutex_lock(&mLock);
    pthread_cond_signal(&mCondition);
    pthread_mutex_unlock(&mLock);
}

